package cn.itsource.org.controller;

import cn.itsource.basic.AjaxResult;
import cn.itsource.basic.PageList;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

//@GetMapping 简单查询
//@PostMapping 高级查询
//@PutMapping  新增，更新
//@DeleteMapping 删除
//@PatchMapping 批量操作
@RequestMapping("/emp")
@RestController //@Controller + @ResponseBody
@Api(tags = "部门接口")
@Validated
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    //http://localhost:8080/dept/id?id=xxx
    //http://localhost:8080/dept/xxx
//    @RequestMapping(value = "/id",method = RequestMethod.GET)
    @GetMapping("/{id}") // = @RequestMapping(value = "/id",method = RequestMethod.GET)
    @ApiOperation("通过id查询部门")
    public Employee queryOne(@PathVariable("id") Long id){
        return employeeService.queryOne(id);
    }

    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }

    @PutMapping
    //contentType = xxx-from    contentType= json
    public AjaxResult addOrUpdate(@RequestBody @Valid Employee employee){
        if(employee.getId() != null){
            employeeService.update(employee);
        }else{
            employeeService.add(employee);
        }
        return AjaxResult.me();
    }

    @DeleteMapping("/{id}") //xxxx/1
    public AjaxResult delete(@PathVariable("id") Long id){
        employeeService.delete(id);
        return AjaxResult.me();
    }

    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * 这里既可以用数组接收也可以用List接收
     * 用什么类型来接收对sql有影响
     * <foreache collection ="list"
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        employeeService.patchDelete(ids);
        return AjaxResult.me();
    }
}
